//
//  WGQConsultationImageTableViewCell.m
//  MaYi
//
//  Created by 小破夕 on 2018/9/5.
//  Copyright © 2018年 JUMI. All rights reserved.
//  首页资讯的单图cell,标注中没有

#import "WGQConsultationImageTableViewCell.h"
#import "UIButton+WebCache.h"

@interface WGQConsultationImageTableViewCell ()

@property (nonatomic,strong) UIView * backView;
@property (nonatomic,strong) UIImageView * iconImageView;//左上角的头像图片
@property (nonatomic,strong) UILabel * nameLabel;//名称label
@property (nonatomic,strong) UILabel * senderLabel;//发送人label
@property (nonatomic,strong) UIButton * closeButton;//右上角的关闭button
@property (nonatomic,strong) UILabel * titleLabel;//标题label
@property (nonatomic,strong) UILabel * contentLabel;//内容label
@property (nonatomic,strong) UIButton * contentImageView;
@property (nonatomic,strong) UILabel * timeLabel;//时间label
@property (nonatomic,strong) UILabel * signLabel;//右下角的标志label
@property (nonatomic,strong) UIImageView * signImageView;//右下角的标志

@property (nonatomic,copy) NSString * contentImageUrl;

@end

@implementation WGQConsultationImageTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = FFColor(0xebebeb);
        [self addSubViews];
    }
    return self;
}

- (void)addSubViews
{
    //背景色，并且有圆角
    self.backView = [[UIView alloc] init];
    self.backView.userInteractionEnabled = YES;
    self.backView.backgroundColor = [UIColor whiteColor];
    self.backView.layer.masksToBounds = YES;
    self.backView.layer.cornerRadius = 7 * kScale;
    [self.contentView addSubview:self.backView];
    
    //左上角的头像图片
    self.iconImageView = [[UIImageView alloc] init];
    self.iconImageView.layer.masksToBounds = YES;
    self.iconImageView.layer.cornerRadius = 20 * kScale;
    [self.contentView addSubview:self.iconImageView];
    
    //名称label
    self.nameLabel = [[UILabel alloc] init];
    self.nameLabel.font = [UIFont fontWithName:kMainFontFormat size:18 * kScale];
    self.nameLabel.textAlignment = NSTextAlignmentLeft;
    self.nameLabel.textColor = FFColor(0x333333);
    [self.contentView addSubview:self.nameLabel];
    
    //发送人label
    self.senderLabel = [[UILabel alloc] init];
    self.senderLabel.font = [UIFont fontWithName:kMainFontFormat size:14 * kScale];
    self.senderLabel.textAlignment = NSTextAlignmentLeft;
    self.senderLabel.textColor = FFColor(0x333333);
    [self.contentView addSubview:self.senderLabel];
    
    
    //右上角的关闭button
    self.closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.closeButton setImage:[UIImage imageNamed:@"closebutton_logo"] forState:UIControlStateNormal];
    [self.closeButton addTarget:self action:@selector(closeButtonActionWithSender:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.closeButton];
    
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.textAlignment = NSTextAlignmentLeft;
    self.titleLabel.font = [UIFont fontWithName:kMainFontFormat size:14 * kScale];
    self.titleLabel.textColor = FFColor(0x333333);
    [self.contentView addSubview:self.titleLabel];
    
    //内容label
    self.contentLabel = [[UILabel alloc] init];
    self.contentLabel.numberOfLines = 2;
    self.contentLabel.textAlignment = NSTextAlignmentLeft;
    self.contentLabel.font = [UIFont fontWithName:kMainFontFormat size:14 * kScale];
    self.contentLabel.textColor = FFColor(0x333333);
    [self.contentView addSubview:self.contentLabel];
    
    self.contentImageView = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentImageView addTarget:self action:@selector(singleTapAction) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.contentImageView];
    
    //时间label
    self.timeLabel = [[UILabel alloc] init];
    self.timeLabel.font = [UIFont fontWithName:kMainFontFormat size:12 * kScale];
    self.timeLabel.textAlignment = NSTextAlignmentLeft;
    self.timeLabel.textColor = FFColor(0x999999);
    [self.contentView addSubview:self.timeLabel];
    
    //右下角的标志label
    self.signLabel = [[UILabel alloc] init];
    self.signLabel.font = [UIFont fontWithName:kMainFontFormat size:10 * kScale];
    self.signLabel.textAlignment = NSTextAlignmentCenter;
    self.signLabel.textColor = FFColor(0x999999);
    [self.contentView addSubview:self.signLabel];
    
    //右下角的标志图片
    self.signImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.signImageView];
}

- (void)singleTapAction
{
    //具体的实现
    if (self.image_select_click)
    {
        self.image_select_click(self.contentImageUrl);
    }
}

- (void)closeButtonActionWithSender:(UIButton *)sender
{
    if (self.delete_click)
    {
        self.delete_click(YES);
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make)
     {
         make.left.equalTo(self.contentView.mas_left).offset(4 * kScale);
         make.right.equalTo(self.contentView.mas_right).offset(-4 * kScale);
         make.top.equalTo(self.contentView.mas_top);
         make.bottom.equalTo(self.contentView.mas_bottom);
     }];
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make)
     {
         make.width.and.height.mas_equalTo(40 * kScale);
         make.top.equalTo(self.contentView.mas_top).offset(12 * kScale);
         make.left.equalTo(self.contentView).offset(16 * kScale);
     }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make)
     {
         make.left.equalTo(self.iconImageView.mas_right).offset(12 * kScale);
         make.top.equalTo(self.iconImageView.mas_top);
     }];
    
    [self.senderLabel mas_makeConstraints:^(MASConstraintMaker *make)
     {
         make.left.equalTo(self.nameLabel);
         make.top.equalTo(self.nameLabel.mas_bottom);
     }];
    
    [self.closeButton mas_makeConstraints:^(MASConstraintMaker *make)
     {
         make.top.equalTo(self.contentView.mas_top).offset(12 * kScale);
         make.right.equalTo(self.contentView.mas_right).offset(-16 * kScale);
         make.width.and.height.mas_equalTo(16 * kScale);
     }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make)
     {
         make.top.equalTo(self.iconImageView.mas_bottom).offset(12 * kScale);
         make.left.equalTo(self.iconImageView.mas_left);
         make.right.equalTo(self.contentView.mas_right).offset(-136 * kScale);
     }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make)
     {
         make.left.equalTo(self.titleLabel.mas_left);
         make.top.equalTo(self.titleLabel.mas_bottom);
         make.right.equalTo(self.titleLabel.mas_right);
     }];
    
    [self.contentImageView mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.width.mas_equalTo(108 * kScale);
        make.right.equalTo(self.contentView.mas_right).offset(-16 * kScale);
        make.top.equalTo(self.titleLabel.mas_top);
        make.height.mas_equalTo(68 * kScale);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make)
     {
         make.top.equalTo(self.contentImageView.mas_bottom).offset(12 * kScale);
         make.left.equalTo(self.contentView.mas_left).offset(16 * kScale);
     }];
    
    [self.signLabel mas_makeConstraints:^(MASConstraintMaker *make)
     {
         make.bottom.equalTo(self.timeLabel.mas_bottom);
         make.right.equalTo(self.contentView.mas_right).offset(-16 * kScale);
     }];
    
    [self.signImageView mas_makeConstraints:^(MASConstraintMaker *make)
     {
         make.bottom.equalTo(self.contentView.mas_bottom).offset(-12 * kScale);
         make.right.equalTo(self.signLabel.mas_left).offset(-4 * kScale);
         make.width.and.height.mas_equalTo(16 * kScale);
     }];
}

- (void)setObject:(id)object
{
    if ([object isKindOfClass:[NSDictionary class]])
    {
        NSDictionary * dict = object;
        NSString *imageUrl = [NSString stringWithFormat:@"%@%@", IMG_OSS, dict[@"sender_img"]];
        [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:imageUrl]];
        
        self.nameLabel.text = dict[@"sender_name"];
        self.senderLabel.text = [NSString stringWithFormat:@"发送人：%@", dict[@"sender_username"]];
//        self.titleLabel.text = model.message_title;
        self.contentLabel.text = dict[@"message_content"];
//        [self.contentImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IMG_OSS,dict[@"message_images"]]]];
        [self.contentImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IMG_OSS,dict[@"message_images"]]] forState:UIControlStateNormal];
        self.contentImageUrl = dict[@"message_images"];
        self.timeLabel.text = dict[@"send_time"];
//        [self.signImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kImageUrl,model.functionimage]]];
//        self.signLabel.text = model.message_type;
    }
    [self layoutIfNeeded];
}

+ (CGFloat)heightWithModel:(NSDictionary *)model
{
    WGQConsultationImageTableViewCell *cell = [[WGQConsultationImageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithUTF8String:object_getClassName([WGQConsultationImageTableViewCell class])]];
    [cell setObject:model];
    [cell layoutIfNeeded];
    CGRect frame = cell.timeLabel.frame;
    return frame.origin.y + frame.size.height + 10 * kScale;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}

@end
